package autoz.core;

import java.io.File;
import java.util.List;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.epsilon.egl.exceptions.EglRuntimeException;
import org.eclipse.epsilon.eol.models.IModel;

/**
 * This class specifies everything necessary to executing the EGL logic file. Each flavour of
 * AUtoZ should implement this class, with the focus on creating a custom postProcess() method.
 * @author jw
 *
 */
public abstract class AbstractAutozModule {
	protected static final String DEFAULT_SOURCE_FILE = "tpl/simpleftlogic.egl";
	protected ILaunchConfiguration configuration;
	
	public AbstractAutozModule(ILaunchConfiguration configuration) {
		this.configuration = configuration;
	}
	
	/**
	 * Returns the location of the template logic file to be executed.
	 * @return
	 */
	protected abstract String getLogicFileLocation();
	
	/**
	 * Returns the file to which the generated Z should be saved.
	 * @return
	 */
	protected abstract File getTargetFile();

	/** 
	 * Grabs the models from the launch configuration
	 * @return
	 */
	protected abstract List<IModel> extractModelsFromConfig();
	
	public void generateToFile() throws Exception {
		
		List<IModel> models = extractModelsFromConfig();
		String logic = getLogicFileLocation();
		AutozEngine engine = new AutozEngine(models, logic);
		try {
			File fileToSave = getTargetFile();
			if (fileToSave.exists()) {			
				engine.generateToFile(getTargetFile());
			} else {
				throw new Exception("Unable to save to file: specified file does not exist.");
			}
		} catch (EglRuntimeException e) {
			throw e;
		}
	}
}
